package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RepositoryDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Repository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = RepositoryService.class)
@Transactional
public class RepositoryServiceImpl implements RepositoryService {
    @Autowired
    private RepositoryDao repositoryDao;

    @Override
    public void add(Repository repository) {
        repositoryDao.add(repository);
    }

    @Override
    public void excelAdd(List<Repository> list) {
        if (list != null && list.size() > 0){
            for (Repository repository : list) {
                repositoryDao.excelAdd(repository);
            }
        }
    }


    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage,pageSize);
        Page<Repository> page = repositoryDao.selectByCondition(queryString);
        long total = page.getTotal();
        List<Repository> rows = page.getResult();
        return new PageResult(total,rows);
    }

    @Override
    public void deleteById(Integer id) {
        repositoryDao.deleteById(id);
    }

    @Override
    public Repository findById(Integer id) {
        return repositoryDao.findById(id);
    }

    @Override
    public void edit(Repository repository) {
        repositoryDao.edit(repository);
    }

    @Override
    public List<Repository> findAll() {
        return repositoryDao.findAll();
    }
}
